package com.ruoyi.camp.interceptor;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Component
public class WSHttpSessionHandshakeInterceptor extends org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor {

    @Autowired
    private TokenService tokenService;

    @Value("${wechart.wxUserRole}")
    private String wxUserRole;

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        if (request instanceof HttpRequest){
            // 解析path中的参数
            String token = ServletUtils.getParameter("token");
            System.out.println("beforeHandshake token: " + token);
            if (StringUtils.isNotEmpty(token)){
                try {
                    LoginUser loginUser = tokenService.getLoginUser(token);
                    attributes.put("token", token);
                    System.out.println("loginUser" + loginUser);
                    if (loginUser != null) {
                        Long roleId = loginUser.getUser().getRoleId();
                        if (roleId == null) {
                            // 后台管理员和客服
                            roleId = 1L;
                        }
                        attributes.put("roleId", roleId);
                        System.out.println(" loginUser.getUser().getRoleId()" + loginUser.getUser().getRoleId());
                        attributes.put("nickName", loginUser.getUser().getNickName());
                        attributes.put("userName", loginUser.getUser().getUserName());


                        String userId;
                        if (Long.valueOf(wxUserRole) == loginUser.getUser().getRoleId()) {
                            userId = loginUser.getUser().getNickName().toString();
                        } else {
                            userId = loginUser.getUser().getUserId().toString();
                        }
                        attributes.put("userId", userId);
                    } else {
                        return false;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return super.beforeHandshake(request,response,wsHandler,attributes);
    }


    private String getUserIdFromToken(String token) {

        String userId = "";



        return userId;
    }
}

